//
//  AppleScrollViewData.m
//  TAOVideo
//
//  Created by Lin Eidan on 13-8-20.
//  Copyright (c) 2013年 taomee. All rights reserved.
//

#import "UserDataBasic.h"
#import "VideoObj.h"
#import "config.h"

#define UserDataHistoryMaxNum  20

@implementation UserDataBasic

#pragma mark 序列化存储

+(void)saveThisObjectToArray:(id)object forArrayKey:(NSString *)arrayKey{
    NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:object];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:arrayKey]];
    if (array == nil) {
        array = [[NSMutableArray alloc] init];
        [[NSUserDefaults standardUserDefaults] setObject:array forKey:arrayKey];
    }
    [array addObject:archiveData];
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:array forKey:arrayKey];
    [userDefault synchronize];
}


+(void)saveThisObjectArray:(NSArray *)objectArray forArrayKey:(NSString *)arrayKey{
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    for (id object in objectArray) {
        NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:object];
        [temp addObject:archiveData];
    }
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:temp forKey:arrayKey];
    [userDefault synchronize];
}

+(NSMutableArray *)readObjectArrayForKey:(NSString *)arrayKey{
    NSArray *myEncodedObject = [[NSUserDefaults standardUserDefaults] objectForKey:arrayKey];
    NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    for (NSData *data in myEncodedObject) {
        id tempObj = [NSKeyedUnarchiver unarchiveObjectWithData: data];
        [resultArray addObject:tempObj];
    }
    return resultArray;
}



#pragma mark 一般存储
+(void)setObject:(id)object forKey:(NSString *)key{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:object forKey:key];
    [userDefault synchronize];
}

+(void)removeObjectForkey:(NSString *)key
{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault removeObjectForKey:key];
    [userDefault synchronize];
}

+(id)getObjectForKey:(NSString *)key
{
    NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
    return [userDefault objectForKey:key];
}

#pragma mark - 播放纪录
+(void)addVideoObjToHistory:(VideoObj *)videoObj{
    NSMutableArray *array=[self readObjectArrayForKey:LocalHistoryData];
    
    for (VideoObj *temp in array) {
        if ([temp.episodeId isEqualToString:videoObj.episodeId]) {
            [array removeObject:temp];
            [array insertObject:videoObj atIndex:0];
            [self saveThisObjectArray:array forArrayKey:LocalHistoryData];
            return;
        }
    }
    if (array.count == UserDataHistoryMaxNum) {
        [array removeObjectAtIndex:UserDataHistoryMaxNum-1];
    }
    [array insertObject:videoObj atIndex:0];
    [self saveThisObjectArray:array forArrayKey:LocalHistoryData];
    
}

+(float)getHasPlayedDurFromHistoryWith:(VideoObj *)obj{
    float hasPlayed=0;
    NSMutableArray *array=[self readObjectArrayForKey:LocalHistoryData];
    for (int i=0; i<array.count ;i++) {
        VideoObj *cur=[array objectAtIndex:i];
        if ([obj.episodeId isEqualToString:cur.episodeId]) {
            hasPlayed = cur.hasPlayDur;
            return hasPlayed;
        }
    }
    return hasPlayed;
}



@end
